﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrameWork.Plugin
{
    /// <summary>
    /// 为插件提供存储池
    /// </summary>
    public static class PluginPool
    {

        /// <summary>
        /// 表示插件列表
        /// </summary>
        private static List<IPlugin> PluginList = new List<IPlugin>();

        private static ConcurrentDictionary<object, object> PluginSharedObjV = new ConcurrentDictionary<object, object>();

        /// <summary>
        /// 表示插件共享的对象集合
        /// </summary>
        public static ConcurrentDictionary<object,object> SharedObjectCollection
        {
            get { return PluginSharedObjV; }
        }

        /// <summary>
        /// 向插件列表中注册插件
        /// </summary>
        /// <param name="Plugin">要注册的插件</param>
        /// <returns>一个值，表示插件是否注册成功</returns>
        public static bool RegisterPlugin(IPlugin Plugin)
        {
            foreach(IPlugin plu in PluginList)
            {
                if (plu.GetPluginGuid().ToString() == Plugin.GetPluginGuid().ToString())
                {
                    return false;
                }
            }
            PluginList.Add(Plugin);
            return true;
        }

        /// <summary>
        /// 向插件列表中注册多个插件
        /// </summary>
        /// <param name="PluginList"></param>
        /// <returns></returns>
        public static bool RegisterPlugin(IEnumerable PluginList)
        {
            bool IsSuc = true ;
            foreach(IPlugin plu in PluginList)
            {
                IsSuc = IsSuc && RegisterPlugin(plu);
            }
            return IsSuc;
        }

        /// <summary>
        /// 获取插件列表
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<IPlugin> GetPlugins()
        {
            return PluginList;
        }

    }
}
